#import "typst-boxes.typ": colorbox

// “预编译”常量
#let __show_note = true // 是否显示笔记

// 常量
// 英文 -> 简中
#let __TRANS_EN_ZHCN = (
  "???": "？？？",
  "Me": "我",
  "Arona": "阿洛娜",
  "Shiroko": "白子",
  "Aru": "阿露",
  "Mutsuki": "睦月",
  "Kayoko": "佳代子",
  "Haruka": "春香",
)

// 产生英文内容
#let en = (content) => (en: content)
#let _en = en
// 产生简体中文内容
#let zh_cn = (content) => (zh_cn: content)
#let _zh = zh_cn


// @param ..contents content|dict 内容。传入格式为 (语言代码: 内容)。
#let dialog = (
  name: none,
  ..contents
) => {
  contents = contents.pos() // 转成数组
  let d = (en: none, zh_cn: none, zh_tw: none, jp: none)
  if type(contents.at(0)) == dictionary {
    // 将 contents 字典数组里的所有字典合并
    for content in contents {
      for (k, v) in content {
        d.insert(k, v)
      }
    }
  }
  else {
    d.insert("en", contents.at(0))
  }
  // 然后对每个语言做判断
  let en_name = name
  let en_content = d.at("en")
  let ret = [*#name*: #en_content]
  if d.at("zh_cn", default: none) != none {
    let zh_name = __TRANS_EN_ZHCN.at(name, default: name)
    let zh_content = d.at("zh_cn")
    ret += [\ *#zh_name*: #zh_content ]
  }
  par(ret)
}

// 显示一个动作，相当于没有名字的 #dialog
#let action = (content, zh_cn: "") => par([#content \ #zh_cn])

// 预定义对话内容
#let arona = dialog.with(name: "Arona")
#let aru = dialog.with(name: "Aru")
#let mutsuki = dialog.with(name: "Mutsuki")
#let shiroko = dialog.with(name: "Shiroko")
#let kayoko = dialog.with(name: "Kayoko")
#let haruka = dialog.with(name: "Haruka")
#let me = dialog.with(name: "Me")

// 对话选择
// @param choices 选择项。不定长参数。
#let choices = (..choices) => {
  let ret = []
  for choice in choices.pos() {
    for lang in choice.values() {
      ret += [#lang \ ]
    }
  }
  align(
    rect(
      par[#ret],
      inset: (left: 20pt, right: 20pt),
      width: 80%
    ),
    center
  )
}

// 显示一个笔记框
// @param content 笔记内容
// @param match 该笔记对应的对话文本语言。默认为“en”。 
#let note = (match: "en", content) => {
  if (not __show_note) { return none }
  colorbox(
    content,
    title: [#emoji.pencil NOTE],
    color: "blue",
    radius: 2pt,
    width: auto,
  )
}

#let def = (match: "en", content) => {
  if (not __show_note) { return none }
  colorbox(
    content,
    title: [#emoji.book DEFINITION],
    color: "red",
    radius: 2pt,
    width: auto,
  )
}

// 显示单词定义
// @param word 单词
// @param type 词性
// @param chinese 中文定义
// @param definition 源语言定义
#let defword = (word, type, chinese, definition) => [*#word* #type. #definition \ #chinese]

#let red(content) = box(fill: yellow, outset: (x: 2pt, y: 0.4em))[#content]
#let hr = align(line(stroke: gray, length: 98%), center)
#let TODO(content) = rect(fill: rgb("#e8d60c"))[*TODO*: #content]